package com.future.hedongjun.mall.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.utils.GlideUtils;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.mall.activity.ShoppingCarActivity;
import com.future.hedongjun.mall.bean.ShoppingCarBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jniu
 * @Email niuxiaoqi@hztianque.com
 * @time 2018/11/19
 */
public class ShoppingCarAdapter extends BaseQuickAdapter<ShoppingCarBean, BaseViewHolder> implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {

    public List<Boolean> isChecked;
    private onPriceChangedListener priceChangedListener;

    public ShoppingCarAdapter(@Nullable List<ShoppingCarBean> data) {
        super(R.layout.recycler_item_shopping_car, data);
        isChecked = new ArrayList<>();
        for (int i = 0; i < getItemCount(); i++) {
            isChecked.add(false);
        }
        setOnItemChildClickListener(this);
        setOnItemClickListener(this);
    }

    public void setPriceChangedListener(onPriceChangedListener priceChangedListener) {
        this.priceChangedListener = priceChangedListener;
    }

    @Override
    public void setNewData(@Nullable List<ShoppingCarBean> data) {
        if (data == null) {
            isChecked = new ArrayList<>();
        } else {
            isChecked = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                isChecked.add(false);
            }
        }
        super.setNewData(data);
    }

    public void setAllChecked(boolean b) {
        if (isChecked == null) {
            return;
        }
        if (b) {
            for (int i = 0; i < isChecked.size(); i++) {
                isChecked.set(i, true);
            }
        } else {
            for (int i = 0; i < isChecked.size(); i++) {
                isChecked.set(i, false);
            }
        }
        if (priceChangedListener != null) {
            priceChangedListener.onPriceChanged(calcPrice());
        }
        notifyDataSetChanged();
    }

    public double calcPrice() {
        double allPrice = 0;
        for (int i = 0; i < isChecked.size(); i++) {
            if (isChecked.get(i)) {
                allPrice += getItem(i).getPrice() * getItem(i).getNum();
            }
        }
        return allPrice;
    }

    @Override
    protected void convert(BaseViewHolder helper, ShoppingCarBean item) {
        helper.setText(R.id.tv_goods_title, item.getShop_name());
        helper.setText(R.id.tv_types, "");
        helper.setText(R.id.tv_price, "¥" + item.getPrice());
        helper.setText(R.id.tv_goods_count, "" + item.getNum());
        helper.setImageResource(R.id.iv_is_check, isChecked.get(helper.getAdapterPosition()) ? R.drawable.gouwuche_xz : R.drawable.gouwuche_mr);
        GlideUtils.loadImage(mContext, item.getImg(), (ImageView) helper.getView(R.id.iv_goods_img));
        helper.addOnClickListener(R.id.iv_add);
        helper.addOnClickListener(R.id.iv_minus);
        helper.addOnClickListener(R.id.iv_delete);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        isChecked.set(position, !isChecked.get(position));
        if (mContext instanceof ShoppingCarActivity) {
            if (isAllChecked()) {
                ((ShoppingCarActivity) mContext).setAllCheck(true);
            } else {
                ((ShoppingCarActivity) mContext).setAllCheck(false);
            }
        }
        notifyItemChanged(position);
        if (priceChangedListener != null) {
            priceChangedListener.onPriceChanged(calcPrice());
        }
    }

    private boolean isAllChecked() {
        boolean b = true;
        if (isChecked != null) {
            for (Boolean aBoolean : isChecked) {
                if (!aBoolean) {
                    b = false;
                    break;
                }
            }
        }
        return b;
    }

    @Override
    public void onItemChildClick(final BaseQuickAdapter adapter, View view, final int position) {
        final ShoppingCarBean item = getItem(position);
        switch (view.getId()) {
            case R.id.iv_add:
                new HttpRequest<DefaultResponse>()
                        .with(mContext)
                        .setApiCode("_upshopcar_001")
                        .addParam("userToken",MyApp.getInstance().getUserToken())
                        .addParam("real_id", item.getReal_id())
                        .addParam("num",item.getNum()+1)
                        .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                            @Override
                            public void onSuccess(BaseResponse response) {
                                item.setNum(item.getNum()+1);
                                notifyDataSetChanged();
                            }
                        }).start(new DefaultResponse());
//                int count = item.getNum();
//
//                count++;
//                item.setNum(count);r
                break;
            case R.id.iv_minus:
                if (item.getNum() > 1) {
                    new HttpRequest<DefaultResponse>()
                            .with(mContext)
                            .setApiCode("_upshopcar_001")
                            .addParam("userToken",MyApp.getInstance().getUserToken())
                            .addParam("real_id", item.getReal_id())
                            .addParam("num", item.getNum() - 1)
                            .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                                @Override
                                public void onSuccess(BaseResponse response) {
                                    item.setNum(item.getNum() - 1);
                                    notifyDataSetChanged();
                                }
                            }).start(new DefaultResponse());
                }
//                int count1 = item.getNum();
//                if (count1 > 1) {
//                    count1--;
//                    item.setNum(count1);
//                }
//                notifyItemChanged(position);
                break;
            case R.id.iv_delete:
                if (item.getNum() > 1) {
                    new HttpRequest<DefaultResponse>()
                            .with(mContext)
                            .setApiCode("_upshopcar_001")
                            .addParam("userToken",MyApp.getInstance().getUserToken())
                            .addParam("real_id", item.getReal_id())
                            .addParam("num", 0)
                            .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                                @Override
                                public void onSuccess(BaseResponse response) {
                                    isChecked.remove(position);
                                    remove(position);
                                    notifyDataSetChanged();
                                }
                            }).start(new DefaultResponse());
                }

                break;
        }
        if (priceChangedListener != null) {
            priceChangedListener.onPriceChanged(calcPrice());
        }
    }

    public interface onPriceChangedListener {
        void onPriceChanged(double price);
    }
}
